/*
 * Copyright (c) 2016 UBG - 版权所有
 * 
 * This software is the confidential and proprietary information of
 * UBG. You shall not disclose such confidential information 
 * and shall use it only in accordance with the terms of the license 
 * agreement you entered into with www.7qjf.com.
 */
package bigdata.mappingproperties;

import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;


/**
 *
 * 描述: properties文件读取工具类
 *
 * @since   v1.0.0
 */
public class PropertiesUtils {

	private static Map<String, Properties> cache = new HashMap<String,Properties>();
	
	private static String ROOT_PATH = PropertiesUtils.class.getResource("/").getPath();
	
	private static Log propertieLog = LogFactory.getLog(PropertiesUtils.class);
	
	/**工具类，不允许实例化**/
	private PropertiesUtils() {}
	
	/**
	 * 
	 *	
	 * 描述:初始化properties
	 *
	 * @created 2016年11月25日 上午11:42:12
	 * @since   v1.0.0 
	 * @param source
	 * @return
	 * @return  Properties
	 */
	public static Properties init(String source) {
		String path = ROOT_PATH + source;
		Properties properties = cache.get(source);
		if(properties == null){
			properties = load(path);
			cache.put(source,properties);
		}
		return  properties;
	}
	
	private static Properties load(String path) {
		Properties prop = new Properties();
		try {
			InputStream in = new FileInputStream(new File(path));
			propertieLog.info("loading ..." + path);
	        prop.load(in);
		} catch (Exception e) {
			propertieLog.error("can't find properties file ... , source = " + path);
			e.printStackTrace();
		}
		return prop;
	}

}
